package tech.edwardvan.rabbitmqdemo.pattern.publishsubscribe;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.extern.slf4j.Slf4j;
import tech.edwardvan.rabbitmqdemo.pattern.CommonUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消息订阅模式-生产者
 *
 * @author EdwardVan
 */
@Slf4j
public class Producer {


    public static void main(String[] args) throws IOException, TimeoutException {
        //获取一个连接
        Connection connection = CommonUtil.getConnection();

        //从连接中获取一个通道
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(CommonUtil.EXCHANGE_PUBLISH_SUBSCRIBE, BuiltinExchangeType.FANOUT, false, true, null);

        //需要发送的消息
        String msg = "hello publish subscribe!";

        for (int i = 0; i < 100; i++) {
            channel.basicPublish(CommonUtil.EXCHANGE_PUBLISH_SUBSCRIBE, "", null, (msg + i).getBytes());
        }

        log.info("生产者发送消息完成");

        //关闭
        channel.close();
        connection.close();
    }
}
